package com.daluzy.chmanager.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.cazaea.sweetalert.SweetAlertDialog;
import com.daluzy.chmanager.R;
import com.daluzy.chmanager.bean.Account;
import com.daluzy.chmanager.bean.Area;
import com.daluzy.chmanager.bean.Manager;
import com.daluzy.chmanager.bean.School;
import com.daluzy.chmanager.util.TelephoneUtil;
import com.xiasuhuei321.loadingdialog.view.LoadingDialog;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;

/**
 * 添加管理员
 */
public class AddAreaActivity extends AppCompatActivity {


    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.et_area_name)
    EditText etAreaName;
    @BindView(R.id.tv_area_school)
    TextView tvAreaSchool;
    @BindView(R.id.tv_add_commit)
    TextView tvAddCommit;
    private String areaName;
    private School school;

    private LoadingDialog loadingDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_area_daluzy);
        ButterKnife.bind(this);

        loadingDialog = new LoadingDialog(this);
    }

    @OnClick({R.id.iv_back, R.id.tv_area_school, R.id.tv_add_commit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_area_school:
                Intent intent = new Intent(AddAreaActivity.this, SelectSchoolActivity.class);
                startActivityForResult(intent, 2021);
                break;
            case R.id.tv_add_commit:
                tvAddCommit.setEnabled(false);
                check();
                break;
        }
    }

    private void check() {
        areaName = etAreaName.getText().toString().trim();
        if (TextUtils.isEmpty(areaName)) {
            Toast.makeText(AddAreaActivity.this, "请输入区域姓名", Toast.LENGTH_SHORT).show();
            tvAddCommit.setEnabled(true);
            return;
        }
        addArea();
    }


    private void addArea() {
        Area area = new Area();
        area.setArea_name(areaName);
        area.setArea_id(String.valueOf(System.currentTimeMillis()));
        if (school != null) {
            area.setArea_school(school.getSchool_name());
        }
        area.save(new SaveListener<String>() {
            @Override
            public void done(String objectId, BmobException e) {
                if (e == null) {
                    Toast.makeText(AddAreaActivity.this, "添加成功", Toast.LENGTH_SHORT).show();
                    finish();
                } else {
                    Toast.makeText(AddAreaActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                }
                if (loadingDialog != null)
                    loadingDialog.close();
                tvAddCommit.setEnabled(true);
            }
        });
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 2021 && resultCode == 2021) {
            school = (School) data.getSerializableExtra("school");
            if (school != null)
                tvAreaSchool.setText(school.getSchool_name());
        }
    }
}
